

# 闭包的特点
# 1、函数中有函数
# 2、内函数必定用到了外函数的参数
# 3、返回值是内函数
# 4、懒函数，只有最终调用内函数时才会进行计算


def balance(unit_price):
    def totoal_price(weight):
        return (weight-0.1)*unit_price
    return totoal_price

apple = balance(5)
'''
等价于
def totoal_price(weight):
    return (weight - 0.1) * 5
并没有执行
'''
app_price = apple(10.1)


print(app_price)

banana = balance(10)
ban_price = banana(6.1)
print(ban_price)

print("==="*100)
print("practice既可以计算平方也可以计算立方")

def practice(x):
    def comp(y):
        if x == 'pingfang':
            return y**2
        if x == 'lifang':
            return y**3
    return comp

pingfang = practice('pingfang')
print(pingfang(2))
lifang = practice('lifang')
print(lifang(2))